在完成數據準備後,我們可以應用不同的篩選邏輯來尋找潛在的交易機會。以下將展示三種不同風格的實戰策略範例。
此策略結合多個技術指標,旨在找出處於強勁上升趨勢的「領導股」。
1. 篩選條件
2. Python 實作
# 安裝所需函式庫
! pip install yahoo_fin
from pandas_datareader import data as pdr
from yahoo_fin import stock_info as si
import yfinance as yf, pandas as pd, datetime, time, os
# 主要邏輯為:先計算所有股票的 RS Rating,篩選出前 70% 的強勢股
# 再對這些強勢股逐一檢查上述其他均線與價格條件
# 檢查 Minervini 條件的核心迴圈
for stock in rs_stocks: # rs_stocks 是已通過 RS Rating 篩選的列表
try:
# (省略讀取與計算 MA 的程式碼)
# 檢查所有條件
condition_1 = currentClose > moving_average_150 > moving_average_200
condition_2 = moving_average_150 > moving_average_200
condition_3 = moving_average_200 > df["SMA_200"][-20] # 200日均線趨勢向上
condition_4 = moving_average_50 > moving_average_150 > moving_average_200
condition_5 = currentClose > moving_average_50
condition_6 = currentClose >= (1.3 * low_of_52week)
condition_7 = currentClose >= (.75 * high_of_52week)
if all([condition_1, condition_2, condition_3, condition_4, condition_5, condition_6, condition_7]):
print(stock + " 滿足 Minervini 的要求")
except Exception as e:
print(f"處理 {stock} 時出錯: {e}")
此策略旨在找出處於上升趨勢、並在技術性回檔至支撐位時出現超賣買入信號的股票。
1. 篩選條件 (策略邏輯)
2. Python 實作
def check_bounce_EMA(df):
candle1 = df.iloc[-1] # 最近 K 線
candle2 = df.iloc[-2] # 前一根 K 線
cond1 = candle1['EMA18'] > candle1['EMA50'] > candle1['EMA100']
cond2 = candle1['STOCH_%K(5,3,3)'] <= 30 or candle1['STOCH_%D(5,3,3)'] <= 30
cond3 = candle2['Low'] < candle2['EMA50'] and candle2['Close'] > candle2['EMA50']
return cond1 and cond2 and cond3
# 遍歷股票清單並進行篩選
for stock_code in stock_list:
try:
price_chart_df = get_stock_price(stock_code)
if check_bounce_EMA(price_chart_df):
print("找到符合條件的股票:", stock_code)
except Exception as e:
print(f"處理 {stock_code} 時發生錯誤: {e}")
此策略結合動能技術指標 (MACD, RSI) 與價值基本面指標 (P/E),旨在尋找兼具成長潛力與價值被低估的股票。
1. 篩選條件
2. Python 實作
# 安裝函式庫
!pip install pandas-ta
import pandas_ta as ta
# 遍歷並篩選股票
for ticker in sp500_tickers:
try:
# 獲取本益比
pe_ratio = get_pe_ratio(ticker)
if pe_ratio is None: continue
# 檢查是否符合所有條件
latest_data = historical_data.iloc[-1]
if (latest_data['RSI14'] > 50) and \
(latest_data['MACD'] > 0) and \
(pe_ratio < 10):
print(f"--- 找到符合條件的股票: {ticker} (P/E: {pe_ratio}) ---")
except Exception as e:
print(f"處理 {ticker} 時發生錯誤: {str(e)}")